//
//  VisitorTableViewController.swift
//  Weibo
//
//  Created by 张婷 on 15/10/16.
//  Copyright © 2015年 itheima. All rights reserved.
//

import UIKit

class VisitorTableViewController: UITableViewController {
    
    // 用户登录标记
    private var logon = UserAccountViewModel.sharedUserAccountModel.userLogon
    
    //private var visitorView = VisitorView()
    var visitorView: VisitorView?
    
    // MARK: - 生命周期函数
    override func loadView() {
        logon ? super.loadView() : setupVisitorView()
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
    }
    
    /// 创建访客视图
    private func setupVisitorView() {
        
        visitorView = VisitorView()
        
        // 设置代理
        //visitorView?.delegate = self
        
        visitorView?.registerButton.addTarget(self, action: "visitorViewDidRegister", forControlEvents: UIControlEvents.TouchUpInside)
        
        visitorView?.loginButton.addTarget(self, action: "visitorViewDidLogin", forControlEvents: UIControlEvents.TouchUpInside)
        
        // 设置全局外观
        UINavigationBar.appearance().tintColor = UIColor.orangeColor()
        
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "注册", style: .Plain, target: self, action: "visitorViewDidRegister")
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "登录", style: .Plain, target: self, action: "visitorViewDidLogin")
        
        view = visitorView
    }
}

// MARK: 访客视图监听方法
extension VisitorTableViewController {
    
    @objc private func visitorViewDidRegister() {
        
        print("注册")
    }
    
    @objc private func visitorViewDidLogin() {
        //print("登录")
        let vc = OAuthViewController()
        let nav = UINavigationController(rootViewController: vc)
        
        presentViewController(nav, animated: true, completion: nil)
    }
}

